package com.cai310.lottery.ticket.protocol.local.disassemble;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cai310.lottery.Constant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.support.pl.PlCompoundContent;
import com.cai310.lottery.support.pl.PlPlayType;
import com.cai310.lottery.ticket.dto.PrintInterfaceDTO;
import com.cai310.lottery.ticket.dto.TicketDTO;
import com.cai310.utils.JsonUtil;

public class PlDisassemble extends AbstractDisassemble {
	private static final NumberFormat TWO_NF = new DecimalFormat("00");
	private static final NumberFormat ONE_NF = new DecimalFormat("0");
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private static final String NUMCODE = ",";
	private static final String CONTENTCONNECTCODE = "-";
	private static final String SINGSPILTCODE = "+";
	@Override
	protected int getMaxMultiple() {
		return 99;
	}

	@Override
	protected int getMaxUnitsKeno() {
		return 5;
	}

	@Override
	protected int getMaxUnitsCommon() {
		return 5;
	}

	@Override
	protected List<TicketDTO> doCompound(PrintInterfaceDTO printData, Date createTime)
			throws Exception {
		
		List<TicketDTO> resultList = new ArrayList<TicketDTO>();
		
		if(printData.getMode()==SalesMode.COMPOUND){
			String[] contents = JsonUtil.getStringArray4Json(printData.getContent());
			for(String content:contents){
				Byte BetType=null;
				Integer PlayType=null;
				StringBuffer BetContent = new StringBuffer();
				PlCompoundContent plCompoundContent = JsonUtil.getObject4JsonString(content, PlCompoundContent.class);
				if(printData.getBetType()==PlPlayType.P5Direct.ordinal()){//排列5直选
					if(plCompoundContent.getUnits()==1){
						//单式
						BetType=1;
						PlayType=1;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea1List(),ONE_NF), ""));
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea2List(),ONE_NF), ""));
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea3List(),ONE_NF), ""));
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea4List(),ONE_NF), ""));
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea5List(),ONE_NF), ""));
					}else{
						BetType=1;
						PlayType=5;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea1List(),ONE_NF), NUMCODE)).append(CONTENTCONNECTCODE);
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea2List(),ONE_NF), NUMCODE)).append(CONTENTCONNECTCODE);
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea3List(),ONE_NF), NUMCODE)).append(CONTENTCONNECTCODE);
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea4List(),ONE_NF), NUMCODE)).append(CONTENTCONNECTCODE);
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea5List(),ONE_NF), NUMCODE));
					}
				}else if(printData.getBetType()==PlPlayType.P3Direct.ordinal()){//排三直选
					if(plCompoundContent.getUnits()==1){
						//单式
						BetType=0;
						PlayType=1;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea1List(),ONE_NF), ""));
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea2List(),ONE_NF), ""));
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea3List(),ONE_NF), ""));
					}else{
						BetType=0;
						PlayType=5;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea1List(),ONE_NF), NUMCODE)).append(CONTENTCONNECTCODE);
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea2List(),ONE_NF), NUMCODE)).append(CONTENTCONNECTCODE);
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getArea3List(),ONE_NF), NUMCODE));
					}
				}else if(printData.getBetType()==PlPlayType.Group3.ordinal()){//排三组三
					if(plCompoundContent.getUnits()==1){
						//单式
						BetType=0;
						PlayType=6;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getGroup3List(),ONE_NF),  ""));
					}else{
						BetType=2;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getGroup3List(),ONE_NF),  NUMCODE));
					}
				}else if(printData.getBetType()==PlPlayType.Group6.ordinal()){//排三组六
					if(plCompoundContent.getUnits()==1){
						//单式
						BetType=0;
						PlayType=6;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getGroup3List(),ONE_NF),  ""));
					}else{
						BetType=3;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getGroup6List(),ONE_NF),  NUMCODE));
					}
				}else if(printData.getBetType()==PlPlayType.DirectSum.ordinal()){//排三直选和值
						BetType=4;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getDirectSumList(),TWO_NF),  NUMCODE));
				}else if(printData.getBetType()==PlPlayType.GroupSum.ordinal()){//排三组选和值
						BetType=5;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getGroupSumList(),ONE_NF),  NUMCODE));
				}else if(printData.getBetType()==PlPlayType.BaoChuan.ordinal()){//包串
						BetType=6;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getBaoChuanList(),ONE_NF),  NUMCODE));
				}else if(printData.getBetType()==PlPlayType.P3DirectKuadu.ordinal()){//直选跨度
						BetType=10;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getDirectKuaduList(),ONE_NF),  NUMCODE));
				}else if(printData.getBetType()==PlPlayType.P3Group3Kuadu.ordinal()){//组选3跨度
						BetType=11;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getG3KuaduList(),ONE_NF),  NUMCODE));
				}else if(printData.getBetType()==PlPlayType.P3Group6Kuadu.ordinal()){//组选6跨度
						BetType=12;
						PlayType=0;
						BetContent.append(StringUtils.join(formatBetNum(plCompoundContent.getG6KuaduList(),ONE_NF),  NUMCODE));
				}
				this.createTicketDTONew(resultList, printData, BetContent.toString(), plCompoundContent.getUnits(), BetType, PlayType);
			}					
		}
		return resultList;
	}
	protected String formatBetNum(String line,NumberFormat nf){
		StringBuffer sb = new StringBuffer();
		String[] arr = line.split(Constant.SINGLE_SEPARATOR_FOR_NUMBER);
		for (int i = 0; i < arr.length; i++) {
			sb.append(nf.format(Integer.valueOf(arr[i])));
		}
		return sb.toString();
	}
	/**
	 * 单式内容拆单操作 可由子类覆写做相应的格式化操作
	 */
	@Override
	protected List<TicketDTO> doSingle(PrintInterfaceDTO printData, Date createTime) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		List<TicketDTO> resultList = new ArrayList<TicketDTO>();
		TicketDTO ticketNew = null;
		Byte BetType=null;
		Integer PlayType=null;
		if(printData.getBetType()==PlPlayType.P5Direct.ordinal()){//排列5直选
				//单式
				BetType=1;
				PlayType=1;
				
		}else if(printData.getBetType()==PlPlayType.P3Direct.ordinal()){//排三直选
				//单式
				BetType=0;
				PlayType=1;
		}else if(printData.getBetType()==PlPlayType.Group3.ordinal()){//排三组三
				//单式
				BetType=0;
				PlayType=6;
		}else if(printData.getBetType()==PlPlayType.Group6.ordinal()){//排三组六
				//单式
				BetType=0;
				PlayType=6;
		}
		
		
		
		int maxUnits = Lottery.isKeno(printData.getLotteryType()) ? this.getMaxUnitsKeno() : this.getMaxUnitsCommon();
		if (printData.getUnits() > maxUnits) {
			String[] arr = printData.getContent().split("(\r\n|\n)+");
			int units = 0;
			List<String> contentList = new ArrayList<String>();
			for (String line : arr) {
				contentList.add(formatBetNum(line,ONE_NF));
				units++;
				if (units == maxUnits) {
					ticketNew = new TicketDTO();
					PropertyUtils.copyProperties(ticketNew, printData);
					ticketNew.setId(null);
					ticketNew.setPrintinterfaceId(printData.getId());
					ticketNew.setCreateTime(createTime);
					ticketNew.setUnits(units);
					ticketNew.setSchemeCost(units * unitsMoney * ticketNew.getMultiple());
					ticketNew.setContent(StringUtils.join(contentList, SINGSPILTCODE));
					ticketNew.setBetType(BetType);
					ticketNew.setExtension(""+PlayType);
					resultList.addAll(this.doMultiple(ticketNew));
					units = 0;// 计数复位
					contentList.clear();// 重置
				}
			}
			if (units > 0) {
				ticketNew = new TicketDTO();
				PropertyUtils.copyProperties(ticketNew, printData);
				ticketNew.setId(null);
				ticketNew.setPrintinterfaceId(printData.getId());
				ticketNew.setCreateTime(createTime);
				ticketNew.setUnits(units);
				ticketNew.setBetType(BetType);
				ticketNew.setExtension(""+PlayType);
				ticketNew.setSchemeCost(units * unitsMoney * ticketNew.getMultiple());
				ticketNew.setContent(StringUtils.join(contentList, SINGSPILTCODE));
				resultList.addAll(this.doMultiple(ticketNew));
			}
		} else {
			ticketNew = new TicketDTO();
			PropertyUtils.copyProperties(ticketNew, printData);
			List<String> contentList = new ArrayList<String>();
			String[] arr = printData.getContent().split("(\r\n|\n)+");
			for (String line : arr) {
				contentList.add(formatBetNum(line,ONE_NF));
			}
			ticketNew.setContent(StringUtils.join(contentList, SINGSPILTCODE));
			ticketNew.setId(null);
			ticketNew.setBetType(BetType);
			ticketNew.setExtension(""+PlayType);
			ticketNew.setPrintinterfaceId(printData.getId());
			ticketNew.setCreateTime(createTime);
			resultList.addAll(this.doMultiple(ticketNew));
		}
		return resultList;
	}
	/**
	 * 
	 * @param printData
	 * @param createTime
	 * @param contentList
	 * @param playType
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	private void createTicketDTONew(List<TicketDTO> resultList ,PrintInterfaceDTO printData,String content,Integer units,Byte betType,Integer playType) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		TicketDTO ticketNew = new TicketDTO();
		PropertyUtils.copyProperties(ticketNew, printData);
		ticketNew.setId(null);
		ticketNew.setPrintinterfaceId(printData.getId());
		ticketNew.setContent(content);
		ticketNew.setUnits(units);
		ticketNew.setBetType(betType);
		ticketNew.setExtension(""+playType);
		ticketNew.setSchemeCost(units * unitsMoney * ticketNew.getMultiple());
		ticketNew.setCreateTime(new Date());
		resultList.addAll(super.doMultiple(ticketNew));
	}
	

	
}
